package com.lishang.location.helper.ui

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.CheckBox
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.widget.addTextChangedListener
import androidx.core.widget.doAfterTextChanged
import androidx.core.widget.doOnTextChanged
import com.lishang.location.helper.R
import com.lishang.location.helper.config.Config
import com.lishang.location.helper.databinding.ActivitySettingBinding
import com.lishang.location.helper.manager.SpManager
import com.lishang.location.helper.receiver.MainReceiver

class SettingActivity : AppCompatActivity() {

    companion object {
        private val LOCATION_PERMISSIONS = arrayOf(
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
        )
        private const val LOCATION_REQUEST_CODE = 1001
    }

    private val bind by lazy {
        ActivitySettingBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(bind.root)

        SpManager.context = application

        bind.checkbox.isChecked = SpManager.getBoolean(Config.KEY_ENABLED)

        bind.toolBar.setNavigationOnClickListener {
            finish()
        }
        bind.checkbox.setOnClickListener {
            MainReceiver.sendEnabled(this, (it as CheckBox).isChecked)
        }

        bind.editOffset.doAfterTextChanged {
            it?.apply {
                if (TextUtils.isEmpty(it)) {
                    MainReceiver.sendOffset(this@SettingActivity, 0)
                } else {
                    MainReceiver.sendOffset(this@SettingActivity, this.toString().toInt())
                }

            }

        }

    }

    override fun onResume() {
        super.onResume()
        bind.txtAddress.text = SpManager.getString(Config.KEY_ADDRESS)
        bind.editOffset.setText(SpManager.getInt(Config.KEY_OFFSET).let {
            if (it == 0) "15" else it.toString()
        })
    }

    fun onLocationClick(view: View) {
        if (checkSelfPermission(LOCATION_PERMISSIONS)) {
            startActivity(Intent(this, MapActivity::class.java))
        } else {
            ActivityCompat.requestPermissions(
                this,
                LOCATION_PERMISSIONS,
                LOCATION_REQUEST_CODE
            )
        }

    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        var isAllGranted = grantResults.let {
            var result = true
            it.forEach {
                if (it != PackageManager.PERMISSION_GRANTED) {
                    result = false
                    return@forEach
                }
            }
            result
        }

        if (isAllGranted.not()) {
            Toast.makeText(this, "缺少相应权限", Toast.LENGTH_SHORT).show()
            return
        }

        when (requestCode) {
            LOCATION_REQUEST_CODE -> {
                startActivity(Intent(this, MapActivity::class.java))
            }
        }
    }

    private fun checkSelfPermission(permissions: Array<String>): Boolean {
        var result = true
        permissions.forEach {
            if (ActivityCompat.checkSelfPermission(
                    this,
                    it
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                result = false
                return@forEach
            }
        }
        return result
    }

}